package com.learn.xueer.app.util;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import java.io.File;

public class UriToFile {

    public static File getFileFromUri(Context context, Uri uri) {
        // 只对"content:" Uri的处理
        String[] filePathColumn = { MediaStore.MediaColumns.DATA };

        ContentResolver contentResolver = context.getContentResolver();
        Cursor cursor = contentResolver.query(uri, filePathColumn, null, null, null);

        if (cursor != null && cursor.moveToFirst()) {
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();

            // 根据路径创建File对象
            return new File(filePath);
        }

        return null;
    }
}
